package com.aretias.jkholdings.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import com.aretias.jkholdings.model.Category;
import com.aretias.jkholdings.model.User;
import com.aretias.jkholdings.service.CategoryService;

/**
 * 
 * @author ryu
 *
 */
public class BaseController {
	
	public final static String key = "userInfo";
	
	@Autowired
	public CategoryService categoryService;
	
	/**
	 * @desc 세션 정보를 가져온다.
	 * @param session
	 * @return
	 */
	public User getUserForSession(HttpSession session){
		return (User) session.getAttribute(key); 
	}
	
	/**
	 * 
	 * @return
	 */
	public List<Category> getFirstDeptCategoryList(){
		Category category = new Category();
		category.setParentCategoryId(0);
		return categoryService.getChild(category);
	}
	
	/**
	 * 
	 */
	public void setPageInfo(ModelAndView mav, int pageSize, int pageBlockSize, int totalCount, int currentPageNumber){
		
		//int currentPageNumber		= param.getPageNumber();
		//int pageSize				= ProductParam.PAGE_SIZE;
		//int pageBlockSize			= ProductParam.PAGE_BLOCK_SIZE;
		//int totalCount 			= productService.getProductsListCount(param);
		int startPageNumber 		= 0;
		int endPageNumber 			= 0;
		boolean previousStatus		= false;
		boolean nextStatus			= false;
		int totalPageCount 			= (int) Math.ceil((double)totalCount / (double)pageSize );
		int totalBlockCount			= (int) Math.ceil((double)totalPageCount / (double)pageBlockSize);
		int currentPageBlockNumber 	= (int) Math.ceil((double)currentPageNumber / (double)pageBlockSize);

		if(totalCount == 0){
			startPageNumber 	= 1;
			endPageNumber 		= 1;
			totalBlockCount 	= 1;
			previousStatus		= false;
			nextStatus			= false;
		}else if(currentPageNumber == 0 || currentPageNumber == 1 || currentPageBlockNumber == 1){
			startPageNumber = 1;
			previousStatus = false;
			
			if(totalPageCount > pageBlockSize){
				endPageNumber = pageBlockSize;
				nextStatus = true;
			}else{
				endPageNumber = totalPageCount;
				nextStatus = false;
			}
		}else{
			previousStatus = true;
			startPageNumber = (currentPageBlockNumber - 1) * pageBlockSize + 1;
			if(totalPageCount == currentPageNumber){
				nextStatus = false;
				endPageNumber = totalPageCount;
			}else{
				nextStatus = true;
				endPageNumber = currentPageBlockNumber * pageBlockSize;
			}
		}
		
		System.out.println("currentPageNumber       : " + currentPageNumber);
		System.out.println("totalCount              : " + totalCount);
		System.out.println("totalPageCount          : " + totalPageCount);
		System.out.println("totalBlockCount         : " + totalBlockCount);
		System.out.println("currentPageBlockNumber 	: " + currentPageBlockNumber);
		System.out.println("startPageNumber         : " + startPageNumber);
		System.out.println("endPageNumber           : " + endPageNumber);
		
		mav.addObject("startPageNumber",startPageNumber);
		mav.addObject("endPageNumber",endPageNumber);
		mav.addObject("previousStatus",previousStatus);
		mav.addObject("nextStatus",nextStatus);
		
	}
}
